package com.zkk.web;

import com.zkk.pojo.User;
import com.zkk.service.UserService;
import com.zkk.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import static java.lang.Integer.parseInt;
@WebServlet("/modify")
public class modifyById extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService userService=new UserServiceImpl();
        HttpSession session = req.getSession();
        User user = (User)session.getAttribute("result");
        String id = (String) session.getAttribute("number");  //通过键值对获取值，注意需要先转出字符串，不能直接转为整型
        String username = req.getParameter("username");      //不然提示整型不能转为字符串
        user.setUsername(username);
        String password = req.getParameter("password");
        user.setPassword(password);                     //先调用通过ID查询单个行的方式，得到值，然后再收参给赋值。因为通过?id方式到网页的值再到Servlet无法传进来
        int i = userService.modifyUser(Integer.valueOf(id), username, password); //然后再转为整型
        resp.sendRedirect("/welcome.html");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
